package com.magnusart.scrumppm.model;

import java.util.Collections;
import java.util.List;

public final class Story implements Comparable<Story>
{
	public enum Units { 
		ONE(1), 
		TWO(2), 
		THREE(3), 
		FIVE(5), 
		EIGHT(8), 
		THIRTEEN(13), 
		UNSPECIFIED(-1);
		
		private final int n;
		
		private Units( int n ) {
			this.n = n;
		}
		
		public int value() {
			return n;
		}
	}
	
	private final int id;
	private final String title;
	private final String description;
	private final String deliverable;
	private final int priority;
	private final Units estimatedUnits;
	private final boolean isDone;
	private final boolean skip;
	private final List<Task> children;
	private boolean hasChild;
	
	public Story( StoryBuilder builder ) {
		if( builder.checkStatus() ) {
			this.id = builder.getID();
			this.title = builder.getTitle();
			this.description = builder.getDescription();
			this.deliverable = builder.getDeliverable();
			this.priority = builder.getPriority();
			this.estimatedUnits = builder.getEstimatedUnits();
			this.isDone = builder.isDone();
			this.skip = builder.skip();
			this.children = builder.getChildren();
			this.hasChild = ( children == null ) ? true : false ;
		} else {
			throw new IllegalStateException( "Builder is in an illegal state." );
		}
	}

	public int getID() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public String getDescription() {
		return description;
	}

	public String getDeliverable() {
		return deliverable;
	}

	public int getPriority() {
		return priority;
	}

	public Units getEstimatedUnits() {
		return estimatedUnits;
	}

	public boolean isDone() {
		return isDone;
	}

	public boolean skip() {
		return skip;
	}

	public List<Task> getChildren() {
		return ( children != null ) ? Collections.unmodifiableList( children ) : null;
	}
	
	public boolean hasTasks() {
		return hasChild;
	}
	
	@Override
	public boolean equals( Object o ) {
		if( o == this )
			return true;
		if( !( o instanceof Story ) )
			return false;
		
		Story story = (Story) o;
		return story.id == id;
	}

	@Override
	public int hashCode() {
		// StoryID is unique in the database, no two duplicates are allowed.
		int result = 23;
		result += 37 + id;
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo( Story story ) {
		// If StoryID are the same the story
		// objects are equal, since the id
		// field is unique in the database. 
		// No two duplicates are allowed.
		return id - story.id;
	}

	@Override
	public String toString() {
		return "[ STORY ID: " + id 
				+ ", Title: " + title 
				+ ", Description" + description 
				+ ", Deliverable: " + deliverable 
				+ ", Priority: " + priority 
				+ ", Etimated Units: " + estimatedUnits
				+ ", Is done: " + isDone
				+ ", Skip: " + skip
				+ ", Has children: " + hasChild
				+ " ]";
	}
}
